package com.example.makerspace.manager;

import com.example.makerspace.dao.CourseVideoDAO;
import com.example.makerspace.domain.CourseDO;
import com.example.makerspace.domain.CourseVideoDO;
import com.example.makerspace.dto.CourseVideoDTO;
import com.example.makerspace.dto.Result;
import com.example.makerspace.enums.ResultCodeEnum;
import com.example.makerspace.utils.FileUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


@Component
public class CourseVideoManager extends BaseManager<CourseVideoDO, CourseVideoDTO> {

    private final static String COURSE_VIDEOS_PATH_PREFIX = "course/videos/";
    @Resource
    private CourseVideoDAO courseVideoDAO;
    @Resource
    private CourseManager courseManager;

   private  final  static  String  devHttpPrefix="https://nism.ytcata.com:443/static/";

  private  final  static  String   testHttpPrefix="https://119.45.199.13:3035/static/";
    @PostConstruct
    public void init(){
        this.baseDAO = courseVideoDAO;
    }

    public Result addCourseVideo(CourseVideoDTO courseVideoDTO) {
        CourseDO courseDO = courseManager.selectById(courseVideoDTO.getCourseId());
        if(courseDO == null){
            return Result.error(ResultCodeEnum.COURSE_NOT_EXIST);
        }
        CourseVideoDO oldCourseVideoDO = courseVideoDAO.selectDuplicate(courseVideoDTO);
        if(oldCourseVideoDO != null){
            return Result.error(ResultCodeEnum.VIDEO_CHAPTER_EXIST);
        }
        CourseVideoDO courseVideoDO = new CourseVideoDO();
        BeanUtils.copyProperties(courseVideoDTO, courseVideoDO);
        String videoPath = FileUtils.saveMultipartFile(courseVideoDTO.getVideo(),
                FileUtils.staticPath+ COURSE_VIDEOS_PATH_PREFIX + courseDO.getCourseName() + "/" + courseVideoDTO.getChapterName());
        courseVideoDO.setVideo(videoPath);
        return addDO(courseVideoDO);
    }

    public Result updateCourseVideo(CourseVideoDTO courseVideoDTO) {
        CourseVideoDO courseVideoDO = courseVideoDAO.selectById(courseVideoDTO.getId());
        if(courseVideoDO == null){
            return Result.error(ResultCodeEnum.VIDEO_CHAPTER_NOT_EXIST);
        }
        CourseDO courseDO = courseManager.selectById(courseVideoDTO.getCourseId());
        if(courseDO == null){
            return Result.error(ResultCodeEnum.COURSE_NOT_EXIST);
        }
        Optional.ofNullable(courseVideoDTO.getChapterName()).ifPresent(courseVideoDO::setChapterName);
        Optional.ofNullable(courseVideoDTO.getDesc()).ifPresent(courseVideoDO::setDesc);
        Optional.ofNullable(courseVideoDTO.getVideo()).ifPresent(video ->{
            FileUtils.deleteFile(courseVideoDO.getVideo());
            String videoPath = FileUtils.saveMultipartFile(courseVideoDTO.getVideo(),
                    FileUtils.staticPath + COURSE_VIDEOS_PATH_PREFIX + courseDO.getCourseName() + "/" + courseVideoDTO.getChapterName());
            courseVideoDO.setVideo(videoPath);
        });
        return updateDO(courseVideoDO);
    }

    @Override
    public Result deleteById(int id){
        CourseVideoDO courseVideoDO = courseVideoDAO.selectById(id);
        if(courseVideoDO == null){
            return Result.error(ResultCodeEnum.VIDEO_CHAPTER_NOT_EXIST);
        }
        String video = courseVideoDO.getVideo();
        FileUtils.deleteFile(video);
        courseVideoDAO.deleteById(id);
        return Result.success();
    }


    public Result detail(CourseVideoDTO courseVideoDTO) {
        CourseDO courseDO = courseManager.selectById(courseVideoDTO.getCourseId());
        if(courseDO == null){
            return Result.error(ResultCodeEnum.COURSE_NOT_EXIST);
        }
        Integer courseId = courseVideoDTO.getCourseId();
        List<CourseVideoDO> courseVideoDOS = courseVideoDAO.selectByCourseId(courseId);
        List<CourseVideoDTO> courseVideoDTOS = Optional.ofNullable(courseVideoDOS).orElse(Collections.emptyList())
                .stream().map(this::convert2DTO).collect(Collectors.toList());
        return Result.success(courseVideoDTOS);
    }

    @Override
    protected CourseVideoDTO convert2DTO(CourseVideoDO courseVideoDO) {
        CourseVideoDTO courseVideoDTO = super.convert2DTO(courseVideoDO);
        String briefPath = FileUtils.getBriefPath(courseVideoDO.getVideo());
        String path = devHttpPrefix+ briefPath;
        courseVideoDTO.setVideoUrl(path);
        return courseVideoDTO;
    }
}
